<?php
/**
 * 文章及文章分类相关函数库
 * ============================================================================
 * 版权所有 2005-2010 KnifeCMS 并保留所有权利。
 * 网站地址: http://www.knifecms.com
 * ----------------------------------------------------------------------------
*/

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}

/**
 * 获得最新的头条文章
 *
 * @access  private
 * @return  array
 */
function get_top_articles($cat_id, $nums = 1)
{
    $sql = 'SELECT article_id, cat_id, title, add_time, pic_url, file_url, open_type, description,content' .
            ' FROM ' . $GLOBALS['ecs']->table('article') .
            ' WHERE is_open = 1 AND cat_id = '. $cat_id .
            ' ORDER BY seq DESC, article_type DESC, article_id DESC LIMIT '. $nums;
    $res = $GLOBALS['db']->getAll($sql);

    $arr = array();
    foreach ($res AS $idx => $row)
    {
        $arr[$idx]['id']          = $row['article_id'];
        if($_CFG['lang']=='zh_cn'){
        	$arr[$idx]['title'] = urlencode(mb_convert_encoding($row['title'], 'utf-8', 'gb2312'));
        	$arr[$idx]['short_title'] = $GLOBALS['_CFG']['article_title_length'] > 0 ? urlencode(mb_convert_encoding(sub_str($row['title'], $GLOBALS['_CFG']['article_title_length']), 'utf-8', 'gb2312')) : urlencode(mb_convert_encoding($row['title'], 'utf-8', 'gb2312'));
        	$arr[$idx]['description'] = urlencode(mb_convert_encoding($row['description'], 'utf-8', 'gb2312'));
			$arr[$idx]['content'] = urlencode(mb_convert_encoding($row['content'], 'utf-8', 'gb2312'));
        }else{
        	$arr[$idx]['title'] = $row['title'];
        	$arr[$idx]['short_title'] = $GLOBALS['_CFG']['article_title_length'] > 0 ? sub_str($row['title'], $GLOBALS['_CFG']['article_title_length']) : mb_convert_encoding($row['title']);
        	$arr[$idx]['description'] = $row['description'];
			$arr[$idx]['content'] = $row['content'];
        }
        $arr[$idx]['add_time']    = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']);
        $arr[$idx]['url']         = build_uri('article', array('aid' => $row['article_id']), $row['title']);
        $arr[$idx]['pic_url']		= trim($row['pic_url']);
		$arr[$idx]['file_url']		= trim($row['file_url']);
        $arr[$idx]['cat_url']     = build_uri('article_cat', array('acid' => $row['cat_id']));
    }
    return $arr;
}

/**
 * 获得最新的头条文章(包含子分类)
 *
 * @access  private
 * @return  array
 */
function get_all_top_articles($cat_id, $nums = 1)
{
	$sub_cats=get_article_children($cat_id);
    $sql = 'SELECT article_id, title, add_time, file_url, open_type, description' .
            ' FROM ' . $GLOBALS['ecs']->table('article') .
            ' WHERE is_open = 1 AND '. $sub_cats .
            ' ORDER BY  article_type DESC, length(seq) DESC,seq DESC, article_id DESC LIMIT '. $nums;
    $res = $GLOBALS['db']->getAll($sql);

    $arr = array();
    foreach ($res AS $idx => $row)
    {
        $arr[$idx]['id']          = $row['article_id'];
        if($_CFG['lang']=='zh_cn'){
        	$arr[$idx]['title'] = urlencode(mb_convert_encoding($row['title'], 'utf-8', 'gb2312'));
        	$arr[$idx]['short_title'] = $GLOBALS['_CFG']['article_title_length'] > 0 ? urlencode(mb_convert_encoding(sub_str($row['title'], $GLOBALS['_CFG']['article_title_length']), 'utf-8', 'gb2312')) : urlencode(mb_convert_encoding($row['title'], 'utf-8', 'gb2312'));
        	$arr[$idx]['description'] = urlencode(mb_convert_encoding($row['description'], 'utf-8', 'gb2312'));
        }else{
        	$arr[$idx]['title'] = $row['title'];
        	$arr[$idx]['short_title'] = $GLOBALS['_CFG']['article_title_length'] > 0 ? sub_str($row['title'], $GLOBALS['_CFG']['article_title_length']) : mb_convert_encoding($row['title']);
        	$arr[$idx]['description'] = $row['description'];
        }
        $arr[$idx]['add_time']    = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']);
        $arr[$idx]['url']         = build_uri('article', array('aid' => $row['article_id']), $row['title']);
				$arr[$idx]['file_url']		= trim($row['file_url']);
        $arr[$idx]['cat_url']     = build_uri('article_cat', array('acid' => $row['cat_id']), $row['cat_name']);
    }
    return $arr;
}

function get_article_subcat($cat_id = 0,$isshow=1)
{
    $tree_arr = array();
    $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('article_cat') . " WHERE parent_id = '$cat_id' ";
    if ($GLOBALS['db']->getOne($sql) || $cat_id == 0)
    {
        $child_sql = 'SELECT cat_id, cat_name, cat_desc, parent_id ' .
                'FROM ' . $GLOBALS['ecs']->table('article_cat') .
                "WHERE parent_id = '$cat_id' ORDER BY sort_order ASC, cat_id ASC";
        $res = $GLOBALS['db']->getAll($child_sql);
        foreach ($res AS $row)
        {
               $tree_arr[$row['cat_id']]['c_id'] = $row['cat_id'];
               $tree_arr[$row['cat_id']]['name'] = $row['cat_name'];
							 $tree_arr[$row['cat_id']]['desc'] = $row['cat_desc'];
               $tree_arr[$row['cat_id']]['url']  = build_uri('article_cat', array('acid' => $row['cat_id']), $row['cat_name']);
           if (isset($row['cat_id']) != NULL){
						$tree_arr[$row['cat_id']]['sub_cat'] = get_article_subcat($row['cat_id']);
					}
        }
    }
    return $tree_arr;
}

/**
 * 获得文章分类下的文章列表
 *
 * @access  public
 * @param   integer     $cat_id
 * @param   integer     $page
 * @param   integer     $size
 *
 * @return  array
 */
function get_cat_articles($cat_id, $page = 1, $size = 20 ,$requirement='')
{
    //取出所有非0的文章
    if ($cat_id == '-1')
    {
        $cat_str = 'cat_id > 0';
    }
    else
    {
        $cat_str = get_article_children($cat_id);
    }
    
 
    //增加搜索条件，如果有搜索内容就进行搜索
    if ($requirement != '')
    {
        $sql = 'SELECT article_id, title, content, author, add_time, pic_url, file_url, open_type, description,images,view_count' .
               ' FROM ' .$GLOBALS['ecs']->table('article') .
               ' WHERE is_open = 1 AND title like \'%' . $requirement . '%\' ' .
               ' ORDER BY article_type DESC,length(seq) DESC, seq DESC, article_id DESC';
    }
    else 
    {
        
        $sql = 'SELECT article_id, title, content, author, add_time, pic_url, file_url, open_type, description,images,view_count' .
               ' FROM ' .$GLOBALS['ecs']->table('article') .
               ' WHERE is_open = 1 AND ' . $cat_str .
               ' ORDER BY article_type DESC,length(seq) DESC, seq DESC, article_id DESC';
    }

    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page-1) * $size);

    $arr = array();
    if ($res)
    {
        while ($row = $GLOBALS['db']->fetchRow($res))
        {
            $article_id = $row['article_id'];

            $arr[$article_id]['id']          = $article_id;
            $arr[$article_id]['title']       = $row['title'];
            $arr[$article_id]['short_title'] = $GLOBALS['_CFG']['article_title_length'] > 0 ? sub_str($row['title'], $GLOBALS['_CFG']['article_title_length']) : $row['title'];
            $arr[$article_id]['author']      = empty($row['author']) || $row['author'] == '_SHOPHELP' ? $GLOBALS['_CFG']['shop_name'] : $row['author'];
            //$arr[$article_id]['url']         = $row['open_type'] != 1 ? build_uri('article', array('aid'=>$article_id), $row['title']) : trim($row['file_url']);
			$arr[$article_id]['url']         = build_uri('article', array('aid'=>$article_id), $row['title']);
			$arr[$article_id]['pic_url']		 = trim($row['pic_url']);
			$arr[$article_id]['file_url']		 = trim($row['file_url']);
                        $arr[$article_id]['add_time']    = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']);
			$arr[$article_id]['description'] = $row['description'];
			$arr[$article_id]['content']		 = $row['content'];
                        if($row["images"]!=null&&$row["images"]!="")
                        {
                            $images_detil =array();
                            $images = explode("|||", $row["images"]);
                             
                           for($i=0;$i<count($images)-1;$i++)
                           {
                               
                                    $img_info  = explode("$$$", $images[$i]); 
                                    $images_detil[$i]['img'] =  $img_info[0];
                                    $images_detil[$i]['info']= $img_info[1];
                                 
                               
                           }
                          
                            $arr[$article_id]['images']=$images_detil;
                        }
        }
    }
    return $arr;
}

/**
 * 获得指定分类下的文章总数
 *
 * @param   integer     $cat_id
 *
 * @return  integer
 */
function get_article_count($cat_id ,$requirement='')
{
    global $db, $ecs;
    if ($requirement != '')
    {
        $count = $db->getOne('SELECT COUNT(*) FROM ' . $ecs->table('article') . ' WHERE ' . get_article_children($cat_id) . ' AND  title like \'%' . $requirement . '%\'  AND is_open = 1');
    }else{
        $count = $db->getOne("SELECT COUNT(*) FROM " . $ecs->table('article') . " WHERE " . get_article_children($cat_id) . " AND is_open = 1");
    }
    return $count;
}

function get_top_articles_by_cat_name($cat_name, $nums = 1){
    $sql = "SELECT article_id, cat_id, title, add_time, pic_url, file_url, open_type, description,content" .
            " FROM " . $GLOBALS['ecs']->table('article') .
            " WHERE is_open = 1 AND cat_id in (SELECT cat_id FROM " . $GLOBALS['ecs']->table('article_cat') .
			" WHERE cat_name = '$cat_name' ) " .
            " ORDER BY seq DESC, article_type DESC, article_id DESC LIMIT ". $nums;
    $res = $GLOBALS['db']->getAll($sql);

    $arr = array();
    foreach ($res AS $idx => $row)
    {
        $arr[$idx]['id']          = $row['article_id'];
        if($_CFG['lang']=='zh_cn'){
        	$arr[$idx]['title'] = urlencode(mb_convert_encoding($row['title'], 'utf-8', 'gb2312'));
        	$arr[$idx]['short_title'] = $GLOBALS['_CFG']['article_title_length'] > 0 ? urlencode(mb_convert_encoding(sub_str($row['title'], $GLOBALS['_CFG']['article_title_length']), 'utf-8', 'gb2312')) : urlencode(mb_convert_encoding($row['title'], 'utf-8', 'gb2312'));
        	$arr[$idx]['description'] = urlencode(mb_convert_encoding($row['description'], 'utf-8', 'gb2312'));
			$arr[$idx]['content'] = urlencode(mb_convert_encoding($row['content'], 'utf-8', 'gb2312'));
        }else{
        	$arr[$idx]['title'] = $row['title'];
        	$arr[$idx]['short_title'] = $GLOBALS['_CFG']['article_title_length'] > 0 ? sub_str($row['title'], $GLOBALS['_CFG']['article_title_length']) : mb_convert_encoding($row['title']);
        	$arr[$idx]['description'] = $row['description'];
			$arr[$idx]['content'] = $row['content'];
        }
        $arr[$idx]['add_time']    = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']);
        $arr[$idx]['url']         = build_uri('article', array('aid' => $row['article_id']), $row['title']);
        $arr[$idx]['pic_url']		= trim($row['pic_url']);
		$arr[$idx]['file_url']		= trim($row['file_url']);
        $arr[$idx]['cat_url']     = build_uri('article_cat', array('acid' => $row['cat_id']));
        $cat_id=$row['cat_id'];
	    if ($cat_id > 0)
	    {
	        $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('article_cat') . ' WHERE cat_id =' . $cat_id;
	        $parent_id = $GLOBALS['db']->getOne($sql);
	    }
	    else
	    {
	        $parent_id = 0;
	    }
	    if ($parent_id > 0){
	    	$arr[$idx]['parent_url']     = build_uri('article_cat', array('acid' => $parent_id));
	    }
    }
    return $arr;
}

function get_sub_cat_by_name($cat_name){
    $tree_arr = array();
    $sql = 'SELECT cat_id FROM ' . $GLOBALS['ecs']->table('article_cat') . " WHERE cat_name = '$cat_name' ";
    if($cat_name!=''){
	    if ($GLOBALS['db']->getOne($sql))
	    {
			$c_id=$GLOBALS['db']->getOne($sql);
			$child_sql = 'SELECT cat_id,cat_name,cat_desc ' .
	                'FROM ' . $GLOBALS['ecs']->table('article_cat') .
	                "WHERE parent_id = '$c_id' ORDER BY sort_order ASC, cat_id ASC";
	        $res = $GLOBALS['db']->getAll($child_sql);
	        foreach ($res AS $row)
	        {
				$tree_arr[$row['cat_id']]['id']   = $row['cat_id'];
				$tree_arr[$row['cat_id']]['name'] = $row['cat_name'];
				$tree_arr[$row['cat_id']]['desc'] = $row['cat_desc'];
				$tree_arr[$row['cat_id']]['url']  = build_uri('article_cat', array('acid' => $row['cat_id']), $row['cat_name']);
				$tree_arr[$row['cat_id']]['sub']  = get_sub_cat_by_name($row['cat_name']);
			}
	    }
    }
    return $tree_arr;
}

function get_sub_cat_by_id($cat_id){
    $tree_arr = array();
    if($cat_id!=''){
		$child_sql = 'SELECT cat_id,cat_name,cat_desc ' .
	                'FROM ' . $GLOBALS['ecs']->table('article_cat') .
	                "WHERE parent_id = '$cat_id' ORDER BY sort_order ASC, cat_id ASC";
		$res = $GLOBALS['db']->getAll($child_sql);
	    foreach ($res AS $row)
	        {
				$tree_arr[$row['cat_id']]['id']   = $row['cat_id'];
				$tree_arr[$row['cat_id']]['name'] = $row['cat_name'];
				$tree_arr[$row['cat_id']]['desc'] = $row['cat_desc'];
				$tree_arr[$row['cat_id']]['url']  = build_uri('article_cat', array('acid' => $row['cat_id']), $row['cat_name']);
				$tree_arr[$row['cat_id']]['sub']  = get_sub_cat_by_id($row['cat_id']);
			}
    }
    return $tree_arr;
}

function get_sub_cat($cat_id = 0,$user_name='')
{
	$subcat_str="";
    $subcat_arr = array();
    $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('article_cat') . ' WHERE (1=1)';
    if($cat_id>=0){
    	$sql .= " and parent_id=" . $cat_id;
    }
    if ($GLOBALS['db']->getOne($sql))
    {
        $child_sql = 'SELECT cat_id, cat_name, cat_desc, parent_id,cat_user ' .
                'FROM ' . $GLOBALS['ecs']->table('article_cat') .
                'WHERE (1=1)';
				if($cat_id>=0){
		    		$child_sql .= " and parent_id=" . $cat_id;
		    	}
				$child_sql .= " ORDER BY sort_order ASC, cat_id ASC";
        $res = $GLOBALS['db']->getAll($child_sql);
        foreach ($res AS $row)
        {
        	$right_cat=true;
        	if(strlen($user_name)>0){
        		if(strpos($row['cat_user'],$user_name)!==false){
        			$right_cat=true;
        		}else{
        			$right_cat=false;
        			$subcat_arr=array_merge($subcat_arr,get_sub_cat($row['cat_id'],$user_name));
        		}
			}
			if($right_cat==true){
				$subcat_arr[$row['cat_id']]['name'] = $row['cat_name'];
	        	$subcat_arr[$row['cat_id']]['id']   = $row['cat_id'];
				if(count(get_sub_cat($row['cat_id']))>0){
					$subcat_arr[$row['cat_id']]['isParent'] = true;
				}else{
					$subcat_arr[$row['cat_id']]['isParent'] = false;
				}
			}
        }
    }
    return $subcat_arr;
}

function get_cat_article_child_tree($tree_id = 0)
{
    $three_arr = array();
    $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('article_cat') . ' WHERE parent_id = ' . $tree_id;
    if ($GLOBALS['db']->getOne($sql) || $tree_id == 0)
    {
        $child_sql = 'SELECT cat_id, cat_name, cat_desc, parent_id ' .
                'FROM ' . $GLOBALS['ecs']->table('article_cat') .
                'WHERE parent_id = ' . $tree_id . ' ORDER BY sort_order ASC, cat_id ASC';
        $res = $GLOBALS['db']->getAll($child_sql);
        foreach ($res AS $row)
        {
			$three_arr[$row['cat_id']]['id']   = $row['cat_id'];
			$three_arr[$row['cat_id']]['name'] = $row['cat_name'];
			$three_arr[$row['cat_id']]['desc'] = $row['cat_desc'];
			$three_arr[$row['cat_id']]['url']  = build_uri('article_cat', array('acid' => $row['cat_id']), $row['cat_name']);
			if (isset($row['cat_id']) != NULL)
			{
				//$three_arr[$row['cat_id']]['cat_id'] = get_cat_article_child_tree($row['cat_id']);
				$three_arr[$row['cat_id']]['subCat'] = get_cat_article_child_tree($row['cat_id']);
			}
        }
    }
    return $three_arr;
}

function get_article_template($article_id){
	global $db, $ecs;
    $tmp = $db->getOne("SELECT template FROM " . $ecs->table('article') . " WHERE article_id=" . $article_id);
    if($tmp==''){
    	$cat_id = $db->getOne("SELECT cat_id FROM " . $ecs->table('article') . " WHERE article_id=" . $article_id);
    	$tmp = $db->getOne("SELECT article_template FROM " . $ecs->table('article_cat') . " WHERE cat_id=" . $cat_id);
    	if($tmp==''){
    		$tmp='article.' . $tpl_suffix;
    	}
    }
    return $tmp;
}

function get_article_cat_template($cat_id){
	global $db, $ecs;
    $tmp = $db->getOne("SELECT template FROM " . $ecs->table('article_cat') . " WHERE cat_id=" . $cat_id);
    if($tmp==''){
    	$tmp='article_cat.' . $tpl_suffix;
    }
    return $tmp;
}

function get_article_cat_type($cat_id){
	global $db, $ecs;
    $tmp = $db->getOne("SELECT show_type FROM " . $ecs->table('article_cat') . " WHERE cat_id=" . $cat_id);
    return $tmp;
}

function get_article_cat_url($cat_id){
	global $db, $ecs;
    $tmp = $db->getOne("SELECT cat_url FROM " . $ecs->table('article_cat') . " WHERE cat_id=" . $cat_id);
    return $tmp;
}

function get_article_cat_name($cat_id){
	global $db, $ecs;
    $tmp = $db->getOne("SELECT cat_name FROM " . $ecs->table('article_cat') . " WHERE cat_id=" . $cat_id);
    return $tmp;
}

function get_article_cat($article_id){
	global $db, $ecs;
    $tmp = $db->getOne("SELECT cat_id FROM " . $ecs->table('article') . " WHERE article_id=" . $article_id);
    return $tmp;
}

function get_article_show_location($cat_id){
	global $db, $ecs;
    $sql="SELECT article_id,open_type,file_url FROM " . $ecs->table('article') . " WHERE cat_id=" . $cat_id;
    $res = $GLOBALS['db']->selectLimit($sql, 1, 0);
    if ($res)
    {
        while ($row = $GLOBALS['db']->fetchRow($res))
        {
        	$article_id = $row['article_id'];
    		$tmp = $row['open_type'] != 1 ? build_uri('article', array('aid'=>$article_id), $row['title']) : trim($row['file_url']);
    	}
    }
    return $tmp;
}

/**
 * 获得指定分类同级的所有分类以及该分类下的子分类
 *
 * @access  public
 * @param   integer     $cat_id     分类编号
 * @return  array
 */
function article_categories_tree($cat_id = 0){
    if ($cat_id > 0)
    {
        $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('article_cat') . ' WHERE cat_id =' . $cat_id;
        $parent_id = $GLOBALS['db']->getOne($sql);
    }
    else
    {
        $parent_id = 0;
    }
    /*
     判断当前分类中是否底级分类，
     如果是取出底级分类上级分类，
     如果不是取当前分类及其下的子分类
    */
    $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('article_cat') . ' WHERE cat_id=' . $parent_id;
    if ($GLOBALS['db']->getOne($sql))
    {
        /* 获取当前分类及其子分类 */
        $sql = 'SELECT a.cat_id, a.cat_name, a.sort_order AS parent_order, a.cat_id, ' .
                    'b.cat_id AS child_id, b.cat_name AS child_name, b.sort_order AS child_order ' .
                'FROM ' . $GLOBALS['ecs']->table('article_cat') . ' AS a ' .
                'LEFT JOIN ' . $GLOBALS['ecs']->table('article_cat') . ' AS b ON b.parent_id = a.cat_id ' .
                'WHERE a.parent_id=' . $parent_id . ' AND a.cat_type=1 ORDER BY parent_order ASC, a.cat_id ASC, child_order ASC';
    }
    else
    {
        /* 获取当前分类及其父分类 */
        $sql = 'SELECT a.cat_id, a.cat_name, b.cat_id AS child_id, b.cat_name AS child_name, b.sort_order ' .
                'FROM ' . $GLOBALS['ecs']->table('article_cat') . ' AS a ' .
                'LEFT JOIN ' . $GLOBALS['ecs']->table('article_cat') . ' AS b ON b.parent_id = a.cat_id ' .
                'WHERE b.parent_id=' . $parent_id . ' AND b.cat_type = 1 ORDER BY sort_order ASC';
    }
    $res = $GLOBALS['db']->getAll($sql);

    $cat_arr = array();
    foreach ($res AS $row)
    {
        $cat_arr[$row['cat_id']]['id']   = $row['cat_id'];
        $cat_arr[$row['cat_id']]['name'] = $row['cat_name'];
        $cat_arr[$row['cat_id']]['url']  = build_uri('article_cat', array('acid' => $row['cat_id']), $row['cat_name']);

        if ($row['child_id'] != NULL)
        {
            $cat_arr[$row['cat_id']]['sub'][$row['child_id']]['id']   = $row['child_id'];
            $cat_arr[$row['cat_id']]['sub'][$row['child_id']]['name'] = $row['child_name'];
            $cat_arr[$row['cat_id']]['sub'][$row['child_id']]['url']  = build_uri('article_cat', array('acid' => $row['child_id']), $row['child_name']);
        }
    }
    return $cat_arr;
}

function get_article_root_tree($cat_id){
	$parent_id=get_article_root($cat_id);
	$sql = 'SELECT a.cat_id, a.cat_name, a.sort_order AS parent_order, a.cat_id, ' .
		'b.cat_id AS child_id, b.cat_name AS child_name, b.sort_order AS child_order ' .
		'FROM ' . $GLOBALS['ecs']->table('article_cat') . ' AS a ' .
		'LEFT JOIN ' . $GLOBALS['ecs']->table('article_cat') . ' AS b ON b.parent_id = a.cat_id AND b.show_in_nav=1 ' .
		'WHERE a.parent_id=' . $parent_id . ' AND a.cat_type=1 AND a.show_in_nav=1 ORDER BY parent_order ASC, a.cat_id ASC, child_order ASC';
		//echo "获取到:".$sql;
    $res = $GLOBALS['db']->getAll($sql);

    $cat_arr = array();
    foreach ($res AS $row)
    {
        $cat_arr[$row['cat_id']]['id']   = $row['cat_id'];
        $cat_arr[$row['cat_id']]['name'] = $row['cat_name'];
        $cat_arr[$row['cat_id']]['url']  = build_uri('article_cat', array('acid' => $row['cat_id']), $row['cat_name']);
        if ($row['child_id'] != NULL)
        {
            $cat_arr[$row['cat_id']]['sub'][$row['child_id']]['id']   = $row['child_id'];
            $cat_arr[$row['cat_id']]['sub'][$row['child_id']]['name'] = $row['child_name'];
            $cat_arr[$row['cat_id']]['sub'][$row['child_id']]['url']  = build_uri('article_cat', array('acid' => $row['child_id']), $row['child_name']);
        }
    }
    return $cat_arr;
}

function get_article_root($cat_id){
	global $db, $ecs;
	$sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('article_cat') . ' WHERE cat_id =' . $cat_id;
	$parent_id = $GLOBALS['db']->getOne($sql);
	if($parent_id>0){
		return get_article_root($parent_id);
	}else{
		return $cat_id;
	}
}

function get_cat_index($cat_id){
	global $db, $ecs;
    if ($cat_id > 0)
    {
        $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('article_cat') . ' WHERE cat_id =' . $cat_id;
        $parent_id = $GLOBALS['db']->getOne($sql);
    }
    else
    {
        $parent_id = 0;
    }
		
}

/**
 * 获得指定文章分类的所有上级分类
 *
 * @access  public
 * @param   integer $cat    分类编号
 * @return  array
 */
function get_article_parent_cats($cat,$show_urhere=0)
{
    if ($cat == 0)
    {
        return array();
    }
    $arr = $GLOBALS['db']->GetAll('SELECT cat_id, cat_name, parent_id,show_urhere FROM ' . $GLOBALS['ecs']->table('article_cat'));

    if (empty($arr))
    {
        return array();
    }

    $index = 0;
    $cats  = array();

    while (1)
    {
        foreach ($arr AS $row)
        {
            if ($cat == $row['cat_id'])
            {
							if($show_urhere>0){
								if($row['show_urhere']>0){
									$cats[$index]['cat_id']   = $row['cat_id'];
									$cats[$index]['cat_name'] = $row['cat_name'];
									$index++;
								}
							}else{
								$cats[$index]['cat_id']   = $row['cat_id'];
								$cats[$index]['cat_name'] = $row['cat_name'];
								$index++;
							}
							$cat = $row['parent_id'];
              break;
            }
        }

        if ($cat == 0)
        {
            break;
        }
    }

    return $cats;
}

?>